<template>
  <div>
    <div class="box">
      <div v-if="user.tenantId && user.tenantId!=''" class="expand-btn" @click="menuDrawer=!menuDrawer">
        <i class="el-icon-menu"/>
      </div>
      <div class="gap"></div>
      <div class="app-name" v-text="user.tenantName">
      </div>
      <div class="right-box">
        <div class="avatar" @click="userDrawer=!userDrawer">
          <el-avatar class="avatar-img" :size="30" src="https://contacts.zoho.com.cn/file?ID=40772320&fs=thumb">
            <img :src="user.avatar"/>
          </el-avatar>
        </div>
        <el-tooltip v-if="imConnectionState!=-1">
          <div slot="content">
            <span>连接状态：</span>
            <span>{{imConnectionState==0?'断开':'正常'}}</span>
          </div>
          <div slot-scope style="cursor: pointer;height:20px;display: inline-flex;align-items: flex-end;margin-left: 10px;">
            <div style="height: 30%;width: 3px;margin-right: 2px;" :style="{background:imConnectionState==0?'red':'#0091ff'}"></div>
            <div style="height: 60%;width: 4px;margin-right: 2px" :style="{background:imConnectionState==0?'red':'#0091ff'}"></div>
            <div style="height: 90%;width: 4px;margin-right: 2px" :style="{background:imConnectionState==0?'red':'#0091ff'}"></div>
          </div>
        </el-tooltip>
      </div>
    </div>
    <el-drawer
        :size="'240px'"
        :with-header="false"
        :visible.sync="menuDrawer"
        direction="ltr"
    >
      <div class="menus">
        <div class="item" @click="menuDrawer = false;$router.push({path:'/'})">
          <div class="icon" :style="{'background-position':'0 -' + 36 * 44.2 + 'px'}"></div>
          <div class="name">首页</div>
        </div>
        <div class="item" @click="menuDrawer = false;$router.push({path:item.path})" v-for="item in menus">
          <div class="icon" :style="{'background-position':'0 -' + item.pos * 44.2 + 'px'}"></div>
          <div class="name" v-text="item.name"></div>
        </div>
      </div>
    </el-drawer>
    <el-drawer
        :size="'240px'"
        :with-header="false"
        :visible.sync="userDrawer"
        direction="rtl"
    >
      <div class="user-drawer">
        <div class="basic-info">
          <div class="avatar">
            <el-avatar class="avatar-img" slot="reference" :size="40" src="https://contacts.zoho.com.cn/file?ID=40772320&fs=thumb">
              <img :src="user.avatar"/>
            </el-avatar>
          </div>
          <div class="info">
            <div class="username">{{user.nickname}}</div>
            <div class="email">{{user.email}}</div>
            <div class="dept">{{user.deptName}}</div>
          </div>
        </div>
        <div class="btn" @click="userDrawer=false;$router.push({name:'setting-user'})">
          修改资料
        </div>
        <div class="btn" style="color: #FF3824" @click="userDrawer=false;logout()">
          注销
        </div>
        <div class=""></div>
      </div>
    </el-drawer>
    <el-dialog :modal-append-to-body="false" title="修改资料" @close="showUserEdit = false" :visible="showUserEdit">
      <EditUser></EditUser>
    </el-dialog>
  </div>
</template>

<script>
import {logout} from '@/api/auth'
import {setToken} from "@/libs/util";
import {disconnect} from "@/api/im";

import EditUser from './components/edit-user'

export default {
  name:'Header',
  components: {
    EditUser
  },
  data(){
    return {
      isFixed:false,
      menuDrawer:false,
      userDrawer:false,
      showUserEdit:false
    }
  },
  methods:{
    logout(){
      setToken('')
      disconnect()
      location.reload()
    }
  },
  computed:{
    user(){
      return this.$store.state.userInfo
    },
    menus(){
      return this.$store.state.menus
    },
    imConnectionState() {
      return this.$store.state.imConnectionState
    }
  }
}
</script>

<style scoped lang="less">

.box {
  width: 100%;
  display: flex;
  height: 40px;
  align-items: center;
  box-shadow: 0 2px 4px 0 var(--cb-color-shadow,rgba(0,0,0,0.16));
  position: fixed;
  top: 0;
  z-index: 1000;
  background: #FFFFFF;

  .gap {
    width: 2px;
    height: 30px;
    background: #E8E8E8;
  }

  .app-name {
    padding: 0 10px;
    line-height: 40px;
    letter-spacing: 2px;
  }

  .expand-btn {
    font-size: 30px;
    padding: 5px;
    transition: all .3s;
    color: #0091FF;
    display: flex;
    align-items: center;
    cursor: pointer;
    //&:hover {
    //  color: #FFFFFF;
    //  background: #0091FF;
    //}
  }

  .right-box {
    flex: 1;
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    > * {
      height: 40px;
      box-sizing: border-box;
      position: relative;
    }
    .avatar {
      padding: 5px 0px;
      margin: 0 10px;
      .avatar-img {
        cursor: pointer;
        width: 34px;
        height: 34px;
        border-radius: 50%;
      }
    }
  }
}
.user-drawer{
  background: #FFF;
  height: 100%;
  position: relative;
  font-size: 14px;

  .basic-info {
    display: flex;
    align-items: self-end;
    padding: 10px;
    border-bottom: 1px solid #F4F4F4;
    .avatar {
      width: 40px;
      height: 40px;
      flex-shrink:0;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .info {
      flex: 1;
      margin-left: 10px;
      word-break: keep-all;
      white-space: nowrap;
      overflow: hidden;
      font-size: 12px;
      .username {
        font-weight: bold;
        font-size: 16px;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .nickname {
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .dept {
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
  .property {
    display: flex;
    padding: 15px;
    line-height: 40px;
    .title {
      margin-right: 10px;
    }
    .content {

    }
  }
  .btn {
    text-align: center;
    line-height: 40px;
    cursor: pointer;
    border-bottom: 1px solid #F4F4F4;
    &:hover {
      background: #F0F0F0;
    }
  }
}

.menus {
  .item {
    cursor: pointer;
    display: flex;
    align-items: center;

    &:hover {
      background: #F0F0F0;
    }
    .icon {
      width: 40px;
      height: 40px;
      margin: 10px;
      flex-shrink: 0;
      background: url('~@/assets/icons/product-icons.png') no-repeat;
      background-size: 100% auto;
    }
    .name {
      flex: 1;
      color: #333;
      margin-left: 10px;
    }
  }
}

.el-drawer__wrapper {
  top: 40px;
}

</style>
